home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Simplest Viewer App / BareBones3DApp.c next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  4.2 KB  |  185 lines  |  [TEXT/CWIE]

  1. /*
  2. **    This is the simplest viewer application reasonable
  3. **
  4. **    Nick Thompson, nickt@apple.com
  5. */
  6.  
  7.  
  8. /*------------------------------------------------------------*/
  9.  
  10. #include <CodeFragments.h>
  11. #include <Dialogs.h>
  12. #include <Fonts.h>
  13. #include <Menus.h>
  14. #include <QuickDraw.h>
  15. #include <SegLoad.h>
  16. #include <StandardFile.h>
  17. #include <Windows.h>
  18.  
  19. #include "QD3DViewer.h"
  20.  
  21. /*------------------------------------------------------------*/
  22.  
  23. /* function prototypes for the application */
  24. Boolean     IsQD3DViewerInstalled( void ) ;
  25. void        MainEventLoop( void ) ;
  26.  
  27.  
  28. /*------------------------------------------------------------*/
  29.  
  30. /* constants */
  31. const    short    kWindHeight = 250 ;
  32. const    short    kWindWidth = 200 ;
  33.  
  34. /*------------------------------------------------------------*/
  35.  
  36. /*
  37. ** this is a simple program, initialise the managers
  38. ** check that the viewer is installed
  39. ** ask for a 3DMF file
  40. ** create a window
  41. ** attach it to a viewer
  42. ** handle events until the window is closed
  43. ** Quit
  44. **
  45. ** No menus, not really an app :-)
  46. */
  47.  
  48. void main(void)
  49. {
  50.  
  51.     short                myNumTypes = 1 ; 
  52.     SFTypeList            myTypeList = { '3DMF' } ;
  53.     StandardFileReply    mySFReply ;
  54.     OSErr                theErr = noErr ;
  55.     short                myRefNum ;
  56.     WindowPtr            myWind = nil ;
  57.     Rect                myRect = { 0, 0, kWindHeight, kWindWidth } ;
  58.     TQ3ViewerObject        myViewer ;
  59.     
  60.     /* Initialize all the needed managers. */
  61.     InitGraf((Ptr)&qd.thePort);
  62.     InitFonts();
  63.     InitWindows();
  64.     InitMenus();
  65.     TEInit();
  66.     InitDialogs((long)nil);
  67.     InitCursor();
  68.     
  69.     /*
  70.     ** must call this so that the heap is expanded to maximum
  71.     ** size before calling any viewer routines
  72.     */
  73.     MaxApplZone() ;
  74.     
  75.     /* we weak linked against the Viewer, let's check that it is installed */
  76.     
  77.     if( (long) Q3ViewerNew != kUnresolvedCFragSymbolAddress ) {
  78.         StandardGetFile( nil, myNumTypes, myTypeList, &mySFReply ) ;
  79.         
  80.         if( mySFReply.sfGood ) {
  81.             theErr = FSpOpenDF( &mySFReply.sfFile,  
  82.                                 fsRdPerm, 
  83.                                 &myRefNum ) ;
  84.                                 
  85.             OffsetRect( &myRect, 50, 50 ) ; 
  86.                               
  87.             myWind = NewCWindow( nil, 
  88.                                  &myRect, 
  89.                                  "\pBareBones3DApp", 
  90.                                  true, 
  91.                                  documentProc, 
  92.                                  (WindowPtr)-1, 
  93.                                  true, 
  94.                                  0L ) ;
  95.                                  
  96.  
  97.             if(myViewer = Q3ViewerNew( (CGrafPtr)myWind, &myWind->portRect, kQ3ViewerDefault )) 
  98.             {
  99.                 /* if the viewer is not nil, we created it ok... */
  100.                 theErr = Q3ViewerUseFile( myViewer, myRefNum );
  101.                 SetWRefCon( myWind, (long)myViewer ) ;        /* stuff the reference in the refcon for later use */
  102.                 MainEventLoop() ;
  103.             }
  104.  
  105.         }
  106.     }
  107.  
  108.     ExitToShell();    
  109.     
  110. }
  111.  
  112.  
  113. /*------------------------------------------------------------*/
  114.  
  115. /*
  116. ** we want to handle events until the fronmost window goes away
  117. */
  118.  
  119. void    MainEventLoop( void ) 
  120. {
  121.     WindowPtr        myWind ;
  122.     Boolean            gotEvent ;
  123.     TQ3ViewerObject    theViewer ;
  124.     OSErr            theErr ;
  125.     RgnHandle        tempRgn ;
  126.     Rect            dragRect ;
  127.     EventRecord        theEvent ;
  128.     GrafPtr            savedPort ;
  129.     
  130.     while(( myWind = FrontWindow()) != nil )
  131.     {
  132.         
  133.         gotEvent = WaitNextEvent( everyEvent, &theEvent, GetCaretTime(), nil ) ;
  134.         if( gotEvent )
  135.         {
  136.             switch( theEvent.what )
  137.             {
  138.               case updateEvt:
  139.                   myWind = (WindowPtr)theEvent.message ;
  140.                 
  141.                 theViewer = (TQ3ViewerObject)GetWRefCon( myWind ) ;
  142.                 BeginUpdate( myWind ) ;
  143.                 theErr = Q3ViewerDraw( theViewer );
  144.                 EndUpdate( myWind ) ;
  145.                 
  146.                 break ;
  147.                 
  148.               case mouseDown:
  149.                 switch( FindWindow( theEvent.where, &myWind ) )
  150.                 {
  151.                   case inGoAway:
  152.                     theViewer = (TQ3ViewerObject)GetWRefCon( myWind ) ;
  153.                       theErr = Q3ViewerDispose( theViewer);
  154.                     DisposeWindow( myWind ) ;
  155.                     break ;
  156.                     
  157.                   case inContent:
  158.                       /*
  159.                       ** There is a bug in versions 1.0.4 and earlier of the Viewer,
  160.                       ** so the port has to be set and restored.
  161.                       */
  162.                     GetPort( &savedPort ) ;
  163.                     SetPort((GrafPtr)myWind ) ;
  164.                 
  165.                     Q3ViewerEvent( theViewer, &theEvent);
  166.                     
  167.                     SetPort( savedPort ) ;
  168.                     break ;
  169.                     
  170.                   case inDrag:
  171.                       tempRgn = GetGrayRgn() ;
  172.                       dragRect = (**tempRgn).rgnBBox ;
  173.                       DragWindow( myWind, theEvent.where, &dragRect ) ;
  174.                     break ;
  175.                 }
  176.                 break ;    
  177.             }
  178.         }
  179.         SetPort(savedPort ) ;
  180.     }    
  181.  
  182. }
  183.  
  184.  
  185.